package com.bytedance.scenedemo.animatiom;

import com.bytedance.scene.Scene;
import com.bytedance.scene.animation.animatorexecutor.NoAnimationExecutor;
import com.bytedance.scene.animation.interaction.InteractionNavigationPopAnimationFactory;
import com.bytedance.scene.animation.interaction.progressanimation.DrawableAnimationBuilder;
import com.bytedance.scene.animation.interaction.progressanimation.InteractionAnimation;
import com.bytedance.scene.animation.interaction.progressanimation.InteractionAnimationBuilder;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.PopOptions;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scene.view.SlidePercentFrameLayout;
import com.bytedance.scenedemo.AnimationListDemoScene;
import com.bytedance.scenedemo.MainScene;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JiangQi on 8/22/18.
 */
public class SlideBackButtonDemoScene extends Scene {
    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_slide_back_button_demo_scene, container, false);
        initView(component);
        return component;
    }

    private void initView(Component component) {
        SlidePercentFrameLayout slidePercentFrameLayout = (SlidePercentFrameLayout) component.findComponentById(ResourceTable.Id_sfl_slide_demo_scene);
        Button btn = (Button) component.findComponentById(ResourceTable.Id_btn_slide);
        InteractionNavigationPopAnimationFactory factory = new InteractionNavigationPopAnimationFactory() {
            @Override
            protected boolean canExit(float progress) {
                return progress > 0.3f;
            }

            @Override
            protected void onInteractionCancel() {
                getView().getBackgroundElement().setAlpha(255);
            }

            @Override
            protected void onInteractionEnd() {
                NavigationSceneGetter.requireNavigationScene(SlideBackButtonDemoScene.this).
                        pop(new PopOptions.Builder().setAnimation(new NoAnimationExecutor()).build());
            }

            @Override
            public boolean isSupport(Scene from, Scene to) {
                return true;
            }

            @Override
            protected List<InteractionAnimation> onPopInteraction(Scene from, Scene to) {
                MainScene mainScene = (MainScene) to;
                AnimationListDemoScene animationListDemoScene = findTargetScene(mainScene);
                int[] buttonLocation = btn.getLocationOnScreen();
                int[] buttonLocation2 = animationListDemoScene.btnIosInteractionAnimation.getLocationOnScreen();
                List<InteractionAnimation> a = new ArrayList<>();
                a.add(
                        InteractionAnimationBuilder.with(btn)
                                .translationXBy((buttonLocation2[0] - buttonLocation[0])).endProgress(0.5f)
                                .build()
                );
                a.add(InteractionAnimationBuilder.with(btn)
                        .translationYBy((buttonLocation2[1] - buttonLocation[1])).endProgress(0.5f)
                        .build()
                );
                getView().getBackgroundElement().setAlpha(0);
                return a;
            }
        };

        slidePercentFrameLayout.setCallback(new SlidePercentFrameLayout.Callback() {
            @Override
            public boolean isSupport() {
                return true;
            }

            @Override
            public void onStart() {
                NavigationSceneGetter.requireNavigationScene(SlideBackButtonDemoScene.this).pop(factory);
                NavigationSceneGetter.requireNavigationScene(SlideBackButtonDemoScene.this).convertBackgroundToBlack();
            }

            @Override
            public void onFinish() {
                factory.finish();
            }

            @Override
            public void onProgress(float progress) {
                factory.updateProgress(progress);
            }
        });
    }

    private AnimationListDemoScene findTargetScene(GroupScene groupScene) {
        List<Scene> childSceneList = groupScene.getSceneList();
        for (Scene scene : childSceneList) {
            if (scene instanceof AnimationListDemoScene) {
                return (AnimationListDemoScene) scene;
            } else if (scene instanceof GroupScene) {
                AnimationListDemoScene animationListDemoScene = findTargetScene((GroupScene) scene);
                if (animationListDemoScene != null) {
                    return animationListDemoScene;
                }
            }
        }
        return null;
    }

}
